package com.vinaprime.primeschedule.databaselayer;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vinaprime.primeschedule.entities.VPContact;

/** * 
 * @author GiangVH
 * @since 27/06/2013
 *
 */
@SuppressLint("NewApi")
public class VPContact_DL {
	//Khai bao cau truc cua table, tuong duong voi cac table o database
	private static final String DB_TABLE_NAME = "VP_CONTACT";
	public static final String DB_TABLE_VPC_ID = "VPC_ID";
	private static final String DB_TABLE_VPC_CODE= "VPC_CODE";
	private static final String DB_TABLE_VPC_LASTNAME ="VPC_LASTNAME";
	public static final String DB_TABLE_VPC_FIRSTNAME ="VPC_FIRSTNAME";
	public static final String DB_TABLE_VPC_EMAIL ="VPC_EMAIL";
	private static final String DB_TABLE_VPC_EMAIL1 ="VPC_EMAIL1";
	private static final String DB_TABLE_VPC_EMAIL2 ="VPC_EMAIL2";	
	private static final String DB_TABLE_VPC_HANDPHONE ="VPC_HANDPHONE";
	private static final String DB_TABLE_VPC_HANDPHONE1 ="VPC_HANDPHONE1";
	public static final String DB_TABLE_VPC_HANDPHONE2 ="VPC_HANDPHONE2";	
	private static final String DB_TABLE_VPC_HOMEPHONE ="VPC_HOMEPHONE";
	private static final String DB_TABLE_VPC_HOMEPHONE1 ="VPC_HOMEPHONE1";
	private static final String DB_TABLE_VPC_HOMEPHONE2 ="VPC_HOMEPHONE2";
	private static final String DB_TABLE_VPC_DESCRIPTION = "VPC_DESCRIPTION";
	private static final String DB_TABLE_VPC_ENABLE_FLAG  ="VPC_ENABLE_FLAG";
	private static final String DB_TABLE_VPC_IMAGE  ="VPC_IMAGE";
	public DatabaseController_DL myDatabaseControllerDL;
	public VPContact_DL (Context context, String path)
	{
		this.myDatabaseControllerDL = new DatabaseController_DL(context, path);
	}
	//Get, seter 
	public DatabaseController_DL getMyDatabaseControllerDL() {
		return myDatabaseControllerDL;
	}
	public void setMyDatabaseControllerDL(
			DatabaseController_DL myDatabaseControllerDL) {
		this.myDatabaseControllerDL = myDatabaseControllerDL;
	}
	public  static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	public static String getDbVpcId() {
		return DB_TABLE_VPC_ID;
	}
	public  static String getDbVpcCode() {
		return DB_TABLE_VPC_CODE;
	}
	public static String getDbVpcLastname() {
		return DB_TABLE_VPC_LASTNAME;
	}
	public static String getDbVpcFirstname() {
		return DB_TABLE_VPC_FIRSTNAME;
	}
	public static String getDbVpcEmail() {
		return DB_TABLE_VPC_EMAIL;
	}
	public static String getDbVpcEmail1() {
		return DB_TABLE_VPC_EMAIL1;
	}
	public static String getDbVpcEmail2() {
		return DB_TABLE_VPC_EMAIL2;
	}
	public static String getDbVpcHandphone() {
		return DB_TABLE_VPC_HANDPHONE;
	}
	public static String getDbVpcHandphone1() {
		return DB_TABLE_VPC_HANDPHONE1;
	}
	public static String getDbVpcHandphone2() {
		return DB_TABLE_VPC_HANDPHONE2;
	}
	public static String getDbVpcHomephone() {
		return DB_TABLE_VPC_HOMEPHONE;
	}
	public static String getDbVpcHomephone1() {
		return DB_TABLE_VPC_HOMEPHONE1;
	}
	public static String getDbVpcHomephone2() {
		return DB_TABLE_VPC_HOMEPHONE2;
	}
	public static String getDbVpcDescription() {
		return DB_TABLE_VPC_DESCRIPTION;
	}
	public static String getDbVpcEnableFlag() {
		return DB_TABLE_VPC_ENABLE_FLAG;
	}
	public static String getDbVpcImage() {
		return DB_TABLE_VPC_IMAGE;
	}
	
	//Cac phuong thuc lien quan
	public Cursor getAllContact()
	{
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		String strSelect = "SELECT * FROM "+DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	}
	public Cursor fetchAllContactsForGroup() {
		 
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		String strSelect = "SELECT "+ DB_TABLE_VPC_ID+", "+DB_TABLE_VPC_FIRSTNAME+", "+DB_TABLE_VPC_EMAIL+"  FROM  "+DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	 }
	
	public Cursor getNewContactID()
	{
		Cursor cur  = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX ("+DB_TABLE_VPC_ID+") AS " +DB_TABLE_VPC_ID+ " FROM "+ DB_TABLE_NAME;
		cur  = db.rawQuery(strQuery, null);
		return cur;
	}
	public Cursor getContactbyId(int Id)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPC_ID +" = " + Id ;
		cur = db.rawQuery(strQuery, null);
		//SQLiteDatabase db = this.myDatabaseControllerDL.getReadableDatabase();
		//String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPC_ID +" = ?" ;
		//cur = db.rawQuery(strQuery, new String[]{String.valueOf(Id)});
		return cur;
	}
	public void AddContact (VPContact contact)	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPC_ID, contact.getVpcID());
		values.put(DB_TABLE_VPC_CODE, contact.getVpcCode());
		values.put(DB_TABLE_VPC_LASTNAME, contact.getVpcLastName());
		values.put(DB_TABLE_VPC_FIRSTNAME, contact.getVpcFistName());		
		values.put(DB_TABLE_VPC_EMAIL, contact.getVpcEmail());
		values.put(DB_TABLE_VPC_EMAIL1, contact.getVpcEmail1());
		values.put(DB_TABLE_VPC_EMAIL2, contact.getVpcEmail2());
		values.put(DB_TABLE_VPC_HANDPHONE,  contact.getVpcHandPhone());
		values.put(DB_TABLE_VPC_HANDPHONE1,  contact.getVpcHandPhone1());
		values.put(DB_TABLE_VPC_HANDPHONE2,  contact.getVpcHandPhone2());
		values.put(DB_TABLE_VPC_HOMEPHONE,  contact.getVpcHomePhone());
		values.put(DB_TABLE_VPC_HOMEPHONE1,  contact.getVpcHomePhone1());
		values.put(DB_TABLE_VPC_HOMEPHONE2,  contact.getVpcHomePhone2());
		values.put(DB_TABLE_VPC_DESCRIPTION,  contact.getVpcDescription());
		values.put(DB_TABLE_VPC_ENABLE_FLAG,  contact.getVpcEnableFlag());
		values.put(DB_TABLE_VPC_IMAGE, contact.getVpcImage());
		db.insert(DB_TABLE_NAME, null, values);		
		db.close();
	}
	public int updateContact (VPContact contact)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL .getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPC_ID, contact.getVpcID());
		values.put(DB_TABLE_VPC_CODE, contact.getVpcCode());
		values.put(DB_TABLE_VPC_LASTNAME, contact.getVpcLastName());
		values.put(DB_TABLE_VPC_FIRSTNAME, contact.getVpcFistName());		
		values.put(DB_TABLE_VPC_EMAIL, contact.getVpcEmail());
		values.put(DB_TABLE_VPC_EMAIL1, contact.getVpcEmail1());
		values.put(DB_TABLE_VPC_EMAIL2, contact.getVpcEmail2());
		values.put(DB_TABLE_VPC_HANDPHONE, contact.getVpcHandPhone());
		values.put(DB_TABLE_VPC_HANDPHONE1, contact.getVpcHandPhone1());
		values.put(DB_TABLE_VPC_HANDPHONE2, contact.getVpcHandPhone2());
		values.put(DB_TABLE_VPC_HOMEPHONE, contact.getVpcHomePhone());
		values.put(DB_TABLE_VPC_HOMEPHONE1, contact.getVpcHomePhone1());
		values.put(DB_TABLE_VPC_HOMEPHONE2, contact.getVpcHomePhone2());
		values.put(DB_TABLE_VPC_DESCRIPTION, contact.getVpcDescription());
		values.put(DB_TABLE_VPC_ENABLE_FLAG,  contact.getVpcEnableFlag());
		values.put(DB_TABLE_VPC_IMAGE, contact.getVpcImage());			
		return db.update(DB_TABLE_NAME, values, DB_TABLE_VPC_ID + "= ?", new String[] {String.valueOf(contact.getVpcID())});
		
	}
	
	public void deleteContact (VPContact contact)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPC_ID + "= ?", new String[] { String.valueOf(contact.getVpcID()) });
		db.close();
	}
	public void deleteContactbyID(int _VpcID){
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPC_ID + "= ?",new String[]{ String.valueOf(_VpcID)});
		db.close();
	}

	
}
